%[H,x]=createGaussianFilter(filterLenSTD,filterLenSamples) - 
%This function returns a 1-D Gaussian filter normalized to unit area.  
%The Gaussian function is fixed to have a std=1 and a mean equal to zero.
%The function returns symmetric filter coefficients as a column vector. Also
%the coefficients are not smoothed at the edges so if the length of the
%filter in standard deviations (filterLenSTD) is 1 then the end points
%of the filter will be sharp so you may want to window the coefficients.
%
%INPUT ARGUMENT
%filterLenSTD - The length of the filter in standard deviations.  This can 
%               be an array if multiple filters are desired.
%filterLenSamples - The filter length in samples.  This should be odd if you 
%                   want the filter symmetric.  If this is an array it must be 
%                   the same size as filterLenSTD.
%
%OUTPUT ARGUMENT
%H - The filter coefficients in a column vector.  If filterLenSTD was an array
%    This will be a cell array with a seperate filter in each cell.
%x - The sample points that the Gaussian was evaluated at. If filterLenSTD was an array
%    This will be a cell array with a seperate filter in each cell.
%
%Author: Paul Otto
function [varargout]=createGaussianFilter(filterLenSTD,filterLenSamples)

filterLenSTD=filterLenSTD(:);
filterLenSamples=filterLenSamples(:);
if ~all(size(filterLenSTD) == size(filterLenSamples))
    error('filterLenSTD and filterLenSamples must have the same number of elements.');
end

std=1;
u=0;

H={};
x={};

for ii=1:length(filterLenSTD)
    stdPlot=[-filterLenSTD(ii):filterLenSTD(ii)];
    x{end+1,1}=linspace(-filterLenSTD(ii),filterLenSTD(ii),filterLenSamples(ii)).';

    H{end+1,1}=exp(-(x{end}-u).^2/(2*std^2))/(sqrt(2*pi)*std); % no reason to scale since will normalize the area to 1.
    H{end}=H{end}/sum(H{end}); 
end

%If there was only one element convert from cell back to a numeric
if  length(H)==1
    H=H{1};
    x=x{1};
end

switch(nargout)
  case 1
    varargout{1}=H;
  case 2
    varargout{1}=H;
    varargout{2}=x;
  otherwise
    error('Invalid number of output arguments');
end
    

end
